package com.yitong.equipment.view.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.ToastUtils
import com.yitong.equipment.R
import com.yitong.equipment.databinding.DialogPrivacyBinding
import kotlin.system.exitProcess

/**
 * @desc 隐私政策弹窗
 */
class PrivacyNewDialog : DialogFragment(), View.OnClickListener {
    private var onClickListener: View.OnClickListener? = null
    private var isCheck = false

    protected var wrap_contentW = false

    private var viewBinding  :DialogPrivacyBinding? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        viewBinding = DialogPrivacyBinding.inflate(LayoutInflater.from(context), container, false)
        initDialog()
        return viewBinding?.root
    }

    private fun initDialog() {
        this.dialog!!.setCanceledOnTouchOutside(false)
        this.dialog!!.requestWindowFeature(1)
        val window = this.dialog!!.window
        window?.setBackgroundDrawable(ColorDrawable(0))
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        bindView()
    }

    private fun bindView() {
        this.dialog?.setOnKeyListener { _, keyCode, _ ->
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                exitProcess(0)
                true
            } else {
                false
            }
        }
        viewBinding!!.tvDisAgree.setOnClickListener(this)
        viewBinding!!.tvAgree.setOnClickListener(this)
        val str = "我已阅读并同意《亿童启蒙用户协议》和《亿童启蒙隐私政策》"
        val spannableBuilder = SpannableStringBuilder(str)
        val colorSpan = ForegroundColorSpan(Color.parseColor("#fcd006"))
        spannableBuilder.setSpan(colorSpan, 7, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        // 单独设置点击事件
        val clickableSpanOne: ClickableSpan = object : ClickableSpan() {
            override fun onClick(view: View) {
//                JumpLocalManager.jumpToWebView(
//                    getActivity(),
//                    Constants.H5_REGISTER,
//                    false,
//                    false,
//                    ""
//                )
            }

            override fun updateDrawState(paint: TextPaint) {
                paint.color = Color.parseColor("#fcd006")
                // 设置下划线 true显示、false不显示
                paint.isUnderlineText = true
            }
        }
        spannableBuilder.setSpan(clickableSpanOne, 7, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        val colorSpan1 = ForegroundColorSpan(Color.parseColor("#fcd006"))
        spannableBuilder.setSpan(colorSpan1, 18, 28, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        // 单独设置点击事件
        val clickableSpanOne1: ClickableSpan = object : ClickableSpan() {
            override fun onClick(view: View) {
//                JumpLocalManager.jumpToWebView(
//                    getActivity(),
//                    Constants.H5_PRIVACY,
//                    false,
//                    false,
//                    ""
//                )
            }

            override fun updateDrawState(paint: TextPaint) {
                paint.color = Color.parseColor("#fcd006")
                // 设置下划线 true显示、false不显示
                paint.isUnderlineText = true
            }
        }
        spannableBuilder.setSpan(clickableSpanOne1, 18, 28, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        viewBinding!!.content.movementMethod = LinkMovementMethod.getInstance()
        viewBinding!!.content.text = spannableBuilder
        viewBinding!!.content.highlightColor = Color.parseColor("#00000000")
        viewBinding!!.content1.text = "如您已阅读、理解并同意上述协议及约定的内容，请勾选协议并点击下方“同意”按钮开始接受我们的服务。"
        viewBinding!!.checkbox.setOnCheckedChangeListener { _, b -> isCheck = b }
    }

    fun setClick(listener: View.OnClickListener?) {
        onClickListener = listener
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.tv_agree -> {
                if (!isCheck) {
                    ToastUtils.showShort("请勾选协议")
                    return
                }
                if (onClickListener != null) {
                    onClickListener?.onClick(v)
                }
                dismiss()
            }
            R.id.tv_disAgree -> exitProcess(0)
        }
    }

    fun show(context: Context) {
        val fragmentManager = (context as FragmentActivity).supportFragmentManager
        this.show(fragmentManager, this.getFragmentTag())
    }

    private fun getFragmentTag(): String? {
        return this.javaClass.simpleName
    }

    override fun onStart() {
        super.onStart()
        val window = this.dialog!!.window
        if (window != null) {
            val params = window.attributes
            params.gravity = 17
            params.width =
                if (this.getWidth() > 0) this.getWidth() else if (this.warpContent()) -2 else -1
            params.height = if (this.getHeight() > 0) this.getHeight() else -2
            window.attributes = params
        }
    }

    private fun warpContent(): Boolean {
        return this.wrap_contentW
    }

    private fun getHeight(): Int {
        return -1
    }

    private fun getWidth(): Int {
        return ScreenUtils.getScreenWidth() * 4 / 5
    }

}